package com.edu.controller;

import net.sf.json.JSONObject;

import java.util.HashMap;

public class R extends HashMap {
	public static R ok(){
		R r = new R();
		r.put("code",0);
		r.put("result","success");
		return r;
	}
	public static R error(Exception e){
		R r = new R();
		r.put("code",500);
		r.put("result","failed");
		r.put("cause",e.getMessage());
		return r;
	}

	public static R data(Object obj){
		R r = new R();
		r.put("code",0);
		r.put("result","success");
		r.put("data",obj);
		return r;
	}

	public static R error(String msg) {
		R r = new R();
		r.put("code",500);
		r.put("result","failed");
		r.put("cause",msg);
		return r;
	}

	public R put(String K, Object V){
		super.put(K, V);
		return this;
	}
	public String toJson(){
		return JSONObject.fromObject( this ).toString();
	}
}
